<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        :border='true'
        :resizable='true'
        :showOverflow='true'
        size='mini'
        :loading='loading'
        :height='pageHeight-seachHeight-90'
        :align='left'
        :columns='columns'
        :data='dataSource'
        :toolbar-config='tableToolbar'
        :checkbox-config="checkboxConfig"
        v-bind='vexConfig'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button type='primary' @click='handleAdd'>新增</a-button>
            <a-button type='primary' @click='batchDel'>删除</a-button>
            <a-button type='primary' :disabled='selectionRows.length!==1' v-has="'business:log'"
                      @click='handleBusiness'>日志
            </a-button>
          </div>
        </template>

        <template #status='{ row }'>
          {{ row.status == 1 ? '启用' : '停用' }}
        </template>

        <template #action='{ row:record }'>
          <a @click='handleAdd(record,"编辑")'>编辑</a>
          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>
              更多
              <a-icon type='down' />
            </a>
            <a-menu slot='overlay'>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handelDeleteAction(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <form-add-edit ref='formAdd' @ok='modalFormOk'></form-add-edit>
    <business-log ref='businessLog'></business-log>
  </a-card>
</template>

<script>
import Vue from 'vue'
import FormAddEdit from '@comp/view/base-crud/form/FormAddEdit'
import { PageListMixin } from '@/mixins/PageListMixin'
import { filterObj } from '@/utils/util'
import { getAction } from '@/api/manage'
import search from '@comp/view/base-crud/search/Search'
export default {
  name: 'product-list',
  mixins: [PageListMixin],
  components: {
    FormAddEdit,
    search
  },
  data() {
    return {
      description: '接口配置',
      importExcelUrl: window._CONFIG['domianURL'] + '/cnProductController/importExcel',
      searchConfig: [
        {
          // label:'订单编号',
          type: 'input',
          name: 'businessName',
          placeholder: '业务名称'
        },
        {
          type: 'a-select',
          name: 'status',
          placeholder: '状态',
          option: [
            {
              name: '启用',
              value: 1
            },
            {
              name: '停用',
              value: 0
            }
          ]
        }
      ],
      //表头
      columns: [],
      //列设置
      settingColumns: [],
      //列定义
      defColumns: [
        { type: 'checkbox', width: 60, fixed: 'left' },

        {
          title: '业务名称',
          field: 'businessName',
          minWidth: 100,
        },
        {
          title: '业务对象',
          minWidth: 100,
          field: 'businessObj'
        },
        {
          title: '最新数据接口',
          minWidth: 100,
          field: 'businessInterfaceUrl'
        },
        {
          title: '备注',
          minWidth: 100,
          field: 'remarks'
        },
        {
          title: '状态',
          align: 'left',
          field: 'status',
          minWidth: 100,
          slots: {
            default: 'status'
          }
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          width: 120,
          slots: {
            default: 'action'
          }
        }
      ],
      url: {
        list: '/busconfig/cnBusinessConfig/findPageList',
        delete: '/busconfig/cnBusinessConfig/delete',
        detail: '/busconfig/cnBusinessConfig/getInfo',
        deleteBatch: '/busconfig/cnBusinessConfig/deleteBatch'
      }
    }
  },
  created() {
    this.initColumns()
  },
  methods: {

    startorstop: function(status) {
      if (!this.url.startOrStopUrl) {
        this.$message.error('请设置url启用停用!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认修改',
          content: '是否修改选中数据?',
          onOk: function() {
            that.loading = true
            getAction(that.url.startOrStopUrl, { ids: ids, status: status }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },

    getQueryParams() {
      //高级查询器
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters)

      param.field = this.getQueryField()
      param.current = this.ipagination.current
      param.size = this.ipagination.pageSize
      delete param.birthdayRange //范围参数不传递后台
      return filterObj(param)
    },


    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ':colsettings'
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues
      const cols = this.defColumns.filter(item => {
        if (item.key == 'rowIndex' || item.field == 'action') {
          return true
        }
        if (this.settingColumns.includes(item.field)) {
          return true
        }
        return false
      })
      this.columns = cols
    },

    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ':colsettings'
      let colSettings = Vue.ls.get(key)
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = []
        this.defColumns.forEach(function(item, i, array) {
          allSettingColumns.push(item.field)
        })
        this.settingColumns = allSettingColumns
        this.columns = this.defColumns
      } else {
        this.settingColumns = colSettings
        const cols = this.defColumns.filter(item => {
          if (item.key == 'rowIndex' || item.field == 'action') {
            return true
          }
          if (colSettings.includes(item.field)) {
            return true
          }
          return false
        })
        this.columns = cols
      }
    },

    async handleAdd(record = {}, title = '新增', disabled = false, disableSubmit = false) {
      if (record.id && this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id })
        if (res.success) {
          if (res.result) {
            record = res.result
          }
        }
      }
      let config = {
        title: title,
        width: 500,
        addOrUpdateUrl: `${window._CONFIG['domianURL']}/busconfig/cnBusinessConfig/saveOrUpdate`,  //审核地址
        method: 'post',
        list: [
          {
            label: '业务名称',
            type: 'input',
            placeholder: '业务名称',
            disabled: disabled,
            name: 'businessName',  // 后台接受字段
            check: { rules: [{ required: true, message: '业务名称' }] }
          },
          {
            label: '业务对象',
            type: 'input',
            disabled: disabled,
            placeholder: '业务对象',
            name: 'businessObj',  // 后台接受字段
            check: { rules: [{ required: true, message: '业务对象' }] }
          },
          {
            label: '最新数据接口',
            type: 'input',
            disabled: disabled,
            placeholder: '最新数据接口',
            name: 'businessInterfaceUrl'  // 后台接受字段
          },
          {
            label: '备注',
            type: 'input',
            disabled: disabled,
            placeholder: '请输入',
            name: 'remarks'  // 后台接受字段
          }

        ]
      }
      this.$refs.formAdd.edit(record, config)
      this.$refs.formAdd.disableSubmit = disableSubmit
    }
  }

}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>